#include "sc/array/sc_array.h"
#include "sc/ini/sc_ini.h"
#include "sc/logger/sc_log.h"

static void example_array(void)
{
    struct sc_array_int xs;

    sc_array_init(&xs);

    sc_array_add(&xs, 0);
    sc_array_add(&xs, 1);
    sc_array_add(&xs, 2);

    int x;
    sc_array_foreach(&xs, x)
    {
        sc_log_debug("log debug: %d\n", x);
    }

    sc_array_clear(&xs);
    for (int i = 0; i < 100; ++i) {
        sc_array_add(&xs, i);
    }

    sc_log_info("size: %zu\n", sc_array_size(&xs));
    sc_log_info("cap: %zu\n", xs.cap);

    sc_array_term(&xs);
}

static int ini_on_item(void* arg, int line, char const* section, char const* key, char const* value)
{
    printf("%03d %s/%s = %s\n", line, section, key, value);
    return 0;
}

static void example_ini(void)
{
    sc_ini_parse_string(
        NULL,
        ini_on_item,

        ("[section1]\n"
         "a = 10\n"
         "b = \"Hello\"\n"
         "\n"
         "# This is comment\n"
         "[section2]\n"
         "c = 9.0\n"));
}

static void log_init(void)
{
    sc_log_init();
    sc_log_set_thread_name("thread-main");

    char const* env_sc_log = getenv("SC_LOG");
    if (env_sc_log != NULL) {
        sc_log_set_level(env_sc_log);
    }
}

int main(void)
{
    log_init();

    sc_log_info("array test\n");
    example_array();

    sc_log_info("ini test\n");
    example_ini();

    sc_log_term();
    return 0;
}
